package com.car.commons.web.aop;

import cn.hutool.json.JSONUtil;
import com.car.commons.web.annotation.AutomotiveServicesLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;


@Component
@Aspect
@Slf4j
public class LogAop {
    @Around("@annotation(com.car.commons.web.annotation.AutomotiveServicesLog)")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        AutomotiveServicesLog declaredAnnotation = signature.getMethod().getDeclaredAnnotation(AutomotiveServicesLog.class);
        log.info("==========开始执行{}==========",declaredAnnotation.value());
        log.info("============参数{}==============", pjp.getArgs());
        //开始执行时间
        long startTime = System.currentTimeMillis();
        Object o = pjp.proceed();
        log.info("=======返回{}======", JSONUtil.parse(o).toString());
        log.info("========耗时{}ms=========",System.currentTimeMillis()-startTime);
        return o;
    }

}
